package com.ibm.cloud.banking.customer.components;

import com.ibm.cloud.banking.constant.TransErrorMgt;
import com.ibm.cloud.banking.customer.entity.CustomerAccountRel;
import com.ibm.cloud.banking.customer.entity.CustomerInfo;
import com.ibm.cloud.banking.customer.repository.ICustomerAccountRelMapper;
import com.ibm.cloud.banking.customer.repository.ICustomerInfoMapper;
import com.ibm.cloud.banking.facade.customer.dto.CustomerAccountRelDTO;
import com.ibm.cloud.banking.platform.exception.BusinessException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class CustomerBasisComponent {

    @Autowired
    ICustomerInfoMapper customerInfoMapper;

    @Autowired
    ICustomerAccountRelMapper customerAccountRelMapper;

//    @Autowired
//    SerialNoServiceFeign serialNoService;

//    @Autowired
//    RedisUtil redisUtil;

    private static final String CUSTOMER_NO_KEY = "id:generator:customer";
    static final String CUSTOMER_NO_TEMPLATE="customerNo";
    private static final String CUSTOMER_TYPE_PRIVATE = "0";
    private static final String GENERATE_CUSTOMER_NO_KEY_TYPE = "type";

    public CustomerInfo selectByUnionKeys(String idType, String idNo, String name) {

        CustomerInfo customerInfo = customerInfoMapper.selectByUnionKeys(idType,
                idNo, name);

        if(customerInfo==null){
            throw new BusinessException(TransErrorMgt.ENTITY_NOT_EXIST.code,
                    String.format(TransErrorMgt.ENTITY_NOT_EXIST.message,"客户信息"));
        }

        return customerInfo;
    }

    public CustomerInfo getCustByNo(String custNo) {

        CustomerInfo customerInfo = customerInfoMapper.selectByPrimaryKey(custNo);

        if(customerInfo==null){
            throw new BusinessException(TransErrorMgt.ENTITY_NOT_EXIST.code,
                    String.format(TransErrorMgt.PARAM_VALUE_IS_NULL.message,"客户信息"));
        }

        return customerInfo;
    }

    public List<CustomerAccountRel> getCustSingleProductAccts(String custNo, String productNo, String acctType) {

        return customerAccountRelMapper.getCustSingleProductAccts(custNo,productNo,acctType);

    }

    @Transactional
    public Integer saveCustAcctRel(CustomerAccountRel custAcctRel) {
        Date createTime = new Date();
        custAcctRel.setCreateDate(createTime);
        custAcctRel.setCreateTime(createTime);
        return customerAccountRelMapper.insertSelective(custAcctRel);

    }

    public String generateCustNo(String type){
//        UidOrderDTO uidOrderDTO = new UidOrderDTO();
//        uidOrderDTO.setAccountTypeCode("IIIIPC");
//        uidOrderDTO.setFixedNo("600000010");
        // return serialNoService.getSerialNo(uidOrderDTO);
//        long sequence = redisUtil.incr(CUSTOMER_NO_KEY,1);
//        String seqStr = String.valueOf(sequence);
//        return String.format("%s%s",StringUtils.repeat("0",10-seqStr.length()),seqStr);
        return null;
    }

    public int saveCustomer(CustomerInfo customerInfo) {
        return customerInfoMapper.insertSelective(customerInfo);
    }

    public int updateCustomer(CustomerInfo customerInfo){
        return customerInfoMapper.updateByPrimaryKeySelective(customerInfo);
    }

    public void initCust(CustomerInfo custDto) {
        customerInfoMapper.insertSelective(custDto);
    }

    public void initCustAcctRel(CustomerAccountRel custAcctRel) {
        customerAccountRelMapper.insertSelective(custAcctRel);
    }

//    public List<QueryCustomerRespDTO> getCustomerByDate(String transDate) {
//        return customerInfoMapper.getCustomerByDate(transDate);
//    }
    
    public List<CustomerAccountRel> selectAcctsByCustNo(String custNo) {
		return customerAccountRelMapper.selectAcctsByCustNo(custNo);
	}

    public int deleteCustomer(String idType, String idNo, String name) {
        return customerInfoMapper.deleteCustomerInfo(idType, idNo, name);
    }

    public List<CustomerAccountRel> selectAcctsByCustNoPagination(String custNo) {
//		return customerAccountRelMapper.selectAcctsByCustNo(custNo,PaginationCriteria.transPagination());
        return customerAccountRelMapper.selectAcctsByCustNo(custNo);
	}

    public void deleteCustAcctRel(CustomerAccountRelDTO custAcctRel) {
        customerAccountRelMapper.deleteCustAcctRel(custAcctRel.getCustomerNo(),custAcctRel.getAccountNo());
    }
}
